home *** CD-ROM | disk | FTP | other *** search
/ Practical Algorithms for Image Analysis / Practical Algorithms for Image Analysis.iso / TARFILE.GZ / tarfile / ch_3.5 / bcd / edge_map.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-09-11  |  3.7 KB  |  160 lines

  1. /*
  2.  * This program is used with permission from I. Cox.
  3.  * Please reference:
  4.  * R. A. Boie, I. Cox, Proc. IEEE 1st Int. Conf. Computer Vision,
  5.  * London, 1987, pp. 450-456.
  6.  */
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <math.h>
  10. #include <malloc.h>
  11. #include "edge_finder.h"
  12.  
  13. extern struct image *my_image;
  14. extern int nxXny;               /*??? */
  15.  
  16. #define _nx my_image->nx
  17. #define image_zc_x(ix,iy,i) \
  18. (((image_locx(iy,ix,i)>0) != (image_locx(iy,ix+1,i+1)>0)) ?EDGE_X:0)
  19.  
  20. #define image_zc_y(ix,iy,i) \
  21. (((image_locy(iy+1,ix,i+_nx)>0) != (image_locy(iy,ix,i)>0)) ?EDGE_Y:0)
  22.  
  23. #define image_zc_135(ix,iy,i) \
  24. (((image_loc135(iy+1,ix+1,i+_nx+1)>0) != (image_loc135(iy,ix,i)>0)) ?EDGE_135:0)
  25.  
  26. #define image_zc_45(ix,iy,i) \
  27. (((image_loc45(iy+1,ix-1,i+_nx-1)>0) != (image_loc45(iy,ix,i)>0)) ?EDGE_45:0)
  28.  
  29. unsigned char *
  30. image_edges (int threshold)
  31. {
  32.   unsigned char *map;
  33.   register int ix, iy, nx, orient_flag, index, ny;
  34.  
  35.   register int *px = my_image->idx;
  36.   register int *py = my_image->idy;
  37.   register int *p45 = my_image->id45;
  38.   register int *p135 = my_image->id135;
  39.   register unsigned char *mapp;
  40.  
  41.   nx = my_image->nx;
  42.   ny = my_image->ny;
  43.   nxXny = nx * ny;
  44.  
  45.   if ((map = (unsigned char *) malloc (ny * nx * sizeof (unsigned char))) == 0) {
  46.     fprintf (stderr, "error: cannot allocate map array\n");
  47.     exit (1);
  48.   }
  49.   mapp = map;
  50.  
  51.   for (iy = 0; iy < ny; iy++) {
  52.     for (ix = 0; ix < nx; ix++) {
  53.       register int sx, sy, s45, s135;
  54.       register int big;
  55.       sx = abs (*px);
  56.       px++;
  57.       sy = abs (*py);
  58.       py++;
  59.       s45 = (int) ((float) (abs (*p45)) / diag_scale);
  60.       p45++;
  61.       s135 = (int) ((float) (abs (*p135)) / diag_scale);
  62.       p135++;
  63.  
  64.       big = sx;
  65.  
  66.       orient_flag = EDGE_X;
  67.  
  68.       if (sy > big) {
  69.         big = sy;
  70.         orient_flag = EDGE_Y;
  71.       }
  72.       if (s45 > big) {
  73.         big = s45;
  74.         orient_flag = EDGE_45;
  75.       }
  76.       if (s135 > big) {
  77.         big = s135;
  78.         orient_flag = EDGE_135;
  79.       }
  80.  
  81.       my_image->orient_flag = orient_flag;
  82.  
  83.       if (big > threshold) {
  84.         index = mapp - map;
  85.         switch (orient_flag) {
  86.         case EDGE_X:
  87.           *mapp++ = image_zc_x (ix, iy, index);
  88.           break;
  89.         case EDGE_Y:
  90.           *mapp++ = image_zc_y (ix, iy, index);
  91.           break;
  92.         case EDGE_45:
  93.           *mapp++ = image_zc_45 (ix, iy, index);
  94.           break;
  95.         case EDGE_135:
  96.           *mapp++ = image_zc_135 (ix, iy, index);
  97.           break;
  98.         default:
  99.           fprintf (stderr, "error in case statements\n");
  100.           exit (1);
  101.           break;
  102.         }
  103.       }
  104.       else {
  105.         *mapp++ = 0;
  106.       }
  107.     }
  108.   }
  109. #ifdef DEBUG
  110.   image_Write_char ("hi_map", map);
  111. #endif
  112.   return (map);
  113. }
  114.  
  115. int
  116. image_edge_map_lo (register int ix, register int iy, register int index)
  117. {
  118.   register int big, sx, sy, s45, s135;
  119.  
  120.   sx = abs (my_image->idx[index]);
  121.   sy = abs (my_image->idy[index]);
  122.   s45 = (int) ((float) (abs (my_image->id45[index])) / diag_scale);
  123.   s135 = (int) ((float) (abs (my_image->id135[index])) / diag_scale);
  124.   big = sx;
  125.  
  126.   my_image->orient_flag = EDGE_X;
  127.  
  128.   if (sy > big) {
  129.     big = sy;
  130.     my_image->orient_flag = EDGE_Y;
  131.   }
  132.   if (s45 > big) {
  133.     big = s45;
  134.     my_image->orient_flag = EDGE_45;
  135.   }
  136.   if (s135 > big) {
  137.     big = s135;
  138.     my_image->orient_flag = EDGE_135;
  139.   }
  140.  
  141.   if (big > my_image->lo_threshold) {
  142.     switch (my_image->orient_flag) {
  143.     case EDGE_X:
  144.       return (image_zc_x (ix, iy, index));
  145.     case EDGE_Y:
  146.       return (image_zc_y (ix, iy, index));
  147.     case EDGE_45:
  148.       return (image_zc_45 (ix, iy, index));
  149.     case EDGE_135:
  150.       return (image_zc_135 (ix, iy, index));
  151.     default:
  152.       fprintf (stderr, "error in case statements\n");
  153.       exit (1);
  154.       break;
  155.     }
  156.   }
  157.   else
  158.     return (0);
  159. }
  160.